﻿using ProductSystem.BLL.Common;
using ProductSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL
{
    public class System_Log_UserBll
    {
        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="curpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public static List<System_Log_User> GetList(string userAccount, int curpage, int pagesize, ref int recordCount)
        {
            List<System_Log_User> list = new List<System_Log_User>();
            using (var dbContext = new EntitiesModel())
            {
                if (string.IsNullOrEmpty(userAccount))
                {
                    list = dbContext.System_Log_Users.OrderByDescending(m => m.Id).Select(t => t).Skip((curpage - 1) * pagesize).Take(pagesize).ToList();
                    recordCount = dbContext.System_Log_Users.Count();
                }
                else
                {
                    list = dbContext.System_Log_Users.Where((m => m.UserAccount.Contains(userAccount))).OrderByDescending(m => m.Id).Select(t => t).Skip((curpage - 1) * pagesize).Take(pagesize).ToList();
                    recordCount = dbContext.System_Log_Users.Where(m => m.UserAccount.Contains(userAccount)).Count();
                }
            }
            return list;
        }
        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static ReturnStatus DeleteSysLog(string ids)
        {
            ReturnStatus rs;
            try
            {
                using (var dbContext = new EntitiesModel())
                {
                    string[] idList = ids.Split(',');
                    for (int i = 0; i < idList.Length; i++)
                    {
                        int id = StringExtension.GetInt(idList[i], 0);
                        System_Log_User model = dbContext.System_Log_Users.FirstOrDefault(o => o.Id == id);
                        dbContext.Delete(model);
                    }
                    dbContext.SaveChanges();
                }
                rs = PromptMessage.Instance().GetPrompt(true, TBConfig.Instance().GetPromptDeleteSuccess);
            }
            catch
            {
                rs = PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptDeleteFail);
            }
            return rs;
        }

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="rs"></param>
        /// <param name="name"></param>
        /// <param name="logcontext"></param>
        public static void SaveLog(bool rs, string name, string logcontext)
        {
            if (rs)
            {
                using (var dbContext = new EntitiesModel())
                {
                    LoginUser currentUser = new LoginUser();
                    currentUser = AppUserSession.Instance().GetCurrentUser();//获取登录用户的信息
                    System_Log_User log = new System_Log_User();
                    log.UserAccount = currentUser.Account;
                    log.ClientIP = currentUser.LoginIp;
                    log.Date = DateTime.Now;
                    log.UserId = (currentUser.UserId).ToString();
                    log.Logger = logcontext;
                    dbContext.Add(log);
                    dbContext.SaveChanges();
                }
            }
        }
    }
}
